/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VistaOrden.java
 *
 * Created on 08/04/2011, 01:01:38 AM
 */

package vistas;

import com.toedter.calendar.JDateChooser;
import controladores.ControladorOrden;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import modelos.ModeloTabla;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 *
 * @author usuario
 */
public class VistaOrden extends javax.swing.JFrame {

    private Vector<JTextField> textos;
    private Vector<JButton> botones;
    private ControladorOrden control;
    private ModeloTabla modeloTabla;
    private Vector<String> identificadores, fila;
    /** Creates new form VistaOrden */
    public VistaOrden() {
        initComponents();
        modeloTabla= new ModeloTabla();
        tblIngrediente.setModel(modeloTabla);

        identificadores= new Vector<String>();

        identificadores.add("Producto");
        identificadores.addElement("Precio");
        identificadores.add("Cantidad");
        identificadores.add("Sub-Total");
        modeloTabla.setColumnIdentifiers(identificadores);

        botones = new Vector<JButton>();
        botones.add(btnSalir);
        botones.add(btnConsultar);
        botones.add(btnGenerar);
        botones.add(btnRegistrar);
        botones.add(btnCancelar);
        botones.add(btnEliminar);
        botones.add(btnMas);
        botones.add(btnMenos);
        textos = new Vector<JTextField>();

        textos.add(txtCodigo);
        textos.add(txtNombre);
        textos.add(txtCantidad);
        textos.add(txtCedula);        
        textos.add(txtTotal);
        txtTotal.setEditable(false);
        control= new ControladorOrden(this);
        btnCancelar.setActionCommand("Cancelar");
        btnConsultar.setActionCommand("Consultar");
        btnRegistrar.setActionCommand("Registrar");
        btnSalir.setActionCommand("Salir");
        btnGenerar.setActionCommand("Generar");
        btnEliminar.setActionCommand("Eliminar");
        btnMas.setActionCommand("Mas");
        btnMenos.setActionCommand("Menos");
        for (JButton boton : botones) {
            boton.addActionListener( control);
        }
        cancelar();
    }

    public void bloquear(){
        txtCodigo.setEditable(false);
        txtCantidad.setEditable(false);
        txtNombre.setEditable(false);
        txtCedula.setEditable(false);
        btnMas.setEnabled(false);
        btnMenos.setEnabled(false);
    }

    public boolean actualizar(){
        for (int i = 0; i < modeloTabla.getRowCount(); i++)
            if(modeloTabla.getValueAt(i, 0).equals(cmbproducto.getItemAt(cmbproducto.getSelectedIndex()))){
               double sub = Double.parseDouble(""+modeloTabla.getValueAt(i, 1))*Integer.parseInt(txtCantidad.getText());
                modeloTabla.setValueAt(txtCantidad.getText(), i, 2);
                modeloTabla.setValueAt(""+sub, i, 3);
               return true;
            }
        return false;
    }

    public boolean validarVacios(){
        for (JTextField texto : textos)
            if(texto.getText().isEmpty())
                return false;
        return true;
    }

    public boolean validarNumeros(){
        for (int i = 2; i < textos.size(); i++)
            try{
                 if(Double.parseDouble(textos.elementAt(i).getText())<0)
                    return false;
            }catch(NumberFormatException ex){
                return false;
            }

        return true;
    }

    public boolean validarFecha(){
        Date d = new Date();
        if(dtdFecha.getDate().before(d))
            return true;
        else
            return false;
    }


    public void agregar(String dato,boolean actu){
        float total,sub;
        if(actu){
            if(!txtTotal.getText().isEmpty())
                total = Float.parseFloat(txtTotal.getText());
            else
                total=0;
            if(!actualizar()){
                fila= new Vector<String>();
                fila.add(dato.substring(0, dato.indexOf("|")));
                fila.add(dato.substring(dato.lastIndexOf("|")+1,dato.length()));
                fila.add(txtCantidad.getText());
                sub = Float.parseFloat(txtCantidad.getText()) * Float.parseFloat(fila.elementAt(1));
                fila.add("" + sub);
                total+=sub;
                txtTotal.setText(""+total);
                modeloTabla.addRow(fila);
            }
        }
        else{
             Vector<String> v = new Vector<String>();
                v.add(dato.substring(0, dato.indexOf("|")));
                v.add(dato.substring(dato.lastIndexOf("|")+1,dato.length()));
                fila= new Vector<String>();
                fila.add(v.elementAt(0));
                fila.add(v.elementAt(1));
                fila.add(txtCantidad.getText());
                sub = Float.parseFloat(txtCantidad.getText()) * Float.parseFloat(v.elementAt(1));
                fila.add("" + sub);
                modeloTabla.addRow(fila);
        }
    }

    public void calcularTotal(){
        float total =0;
        for (int i = 0; i < modeloTabla.getRowCount(); i++) 
            total+=Float.parseFloat(""+modeloTabla.getValueAt(i, 3));
        txtTotal.setText(""+total);
    }

    public void eliminar(){
        int i = tblIngrediente.getSelectedRow();
        if(i != -1){
            modeloTabla.removeRow(i);
        }
        else
            JOptionPane.showMessageDialog(new JFrame(),"Debe seleccionar la fila que desea eliminar","Burguer",JOptionPane.ERROR_MESSAGE);

    }

     public void cancelar(){
         dtdFecha.setDate(new Date());
        for (JTextField texto : textos){
            texto.setEditable(true);
            texto.setText("");
        }
        txtCodigo.setEditable(true);
        txtTotal.setEditable(true);
        btnMas.setEnabled(true);
        btnMenos.setEnabled(true);
        txtNombre.setEditable(true);
        txtCedula.setEditable(true);
        modeloTabla.setRowCount(0);
     }

    public void cargarComboProductos(Vector<String> descripciones){
        cmbproducto.removeAllItems();
        for (int i = 0; i < descripciones.size(); i++) {
            cmbproducto.addItem(descripciones.elementAt(i));

        }
    }

    public Vector<JTextField> getTextos() {
        return textos;
    }

    public JTextField getTxtCantidad() {
        return txtCantidad;
    }

    public JTextField getTxtCedula() {
        return txtCedula;
    }

    public JTextField getTxtCodigo() {
        return txtCodigo;
    }

    public JTextField getTxtNombre() {
        return txtNombre;
    }

    public JTextField getTxtTotal() {
        return txtTotal;
    }

    public JDateChooser getDtdFecha() {
        return dtdFecha;
    }

    

    public String getTextoCmbProducto() {
        return (String) cmbproducto.getItemAt(cmbproducto.getSelectedIndex());
    }

    public Vector<String> getInfoTabla(){
         Vector<String> v = new Vector<String>();
        for (int i = 0; i < modeloTabla.getRowCount(); i++)
            v.add(""+modeloTabla.getValueAt(i, 0)+"|"+modeloTabla.getValueAt(i, 2));
        return v;
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnConsultar = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        txtCodigo = new javax.swing.JTextField();
        cmbproducto = new javax.swing.JComboBox();
        btnGenerar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        txtCedula = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtTotal = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        btnMas = new javax.swing.JButton();
        btnMenos = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblIngrediente = new javax.swing.JTable();
        btnRegistrar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        dtdFecha = new com.toedter.calendar.JDateChooser();
        jLabel7 = new javax.swing.JLabel();
        txtCantidad = new javax.swing.JTextField();

        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(null);

        btnConsultar.setFont(new java.awt.Font("Arial", 1, 15));
        btnConsultar.setText("Consultar");
        getContentPane().add(btnConsultar);
        btnConsultar.setBounds(480, 20, 120, 30);

        jLabel6.setFont(new java.awt.Font("Arial", 0, 15));
        jLabel6.setText("Codigo:");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(160, 30, 60, 30);

        txtCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCodigoActionPerformed(evt);
            }
        });
        getContentPane().add(txtCodigo);
        txtCodigo.setBounds(280, 30, 190, 30);

        getContentPane().add(cmbproducto);
        cmbproducto.setBounds(110, 160, 190, 30);

        btnGenerar.setFont(new java.awt.Font("Arial", 1, 15));
        btnGenerar.setText("Generar");
        getContentPane().add(btnGenerar);
        btnGenerar.setBounds(480, 50, 120, 30);

        jLabel2.setFont(new java.awt.Font("Arial", 0, 13));
        jLabel2.setText("Ciente:");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(160, 70, 140, 32);

        txtNombre.setFont(new java.awt.Font("Arial", 0, 13));
        getContentPane().add(txtNombre);
        txtNombre.setBounds(280, 70, 190, 32);

        txtCedula.setFont(new java.awt.Font("Arial", 0, 13));
        getContentPane().add(txtCedula);
        txtCedula.setBounds(280, 110, 190, 30);

        jLabel3.setFont(new java.awt.Font("Arial", 0, 13));
        jLabel3.setText("Cedula:");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(160, 110, 224, 32);

        txtTotal.setFont(new java.awt.Font("Arial", 0, 13));
        getContentPane().add(txtTotal);
        txtTotal.setBounds(610, 380, 110, 30);

        jLabel5.setFont(new java.awt.Font("Arial", 0, 13));
        jLabel5.setText("Total:");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(550, 380, 55, 30);

        btnMas.setFont(new java.awt.Font("Arial", 1, 15));
        btnMas.setText("+");
        getContentPane().add(btnMas);
        btnMas.setBounds(260, 210, 50, 30);

        btnMenos.setFont(new java.awt.Font("Arial", 1, 15));
        btnMenos.setText("-");
        getContentPane().add(btnMenos);
        btnMenos.setBounds(350, 210, 50, 30);

        tblIngrediente.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tblIngrediente);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(100, 250, 440, 160);

        btnRegistrar.setFont(new java.awt.Font("Arial", 1, 15));
        btnRegistrar.setText("Registrar");
        btnRegistrar.setPreferredSize(new java.awt.Dimension(80, 30));
        getContentPane().add(btnRegistrar);
        btnRegistrar.setBounds(70, 430, 120, 30);

        btnEliminar.setFont(new java.awt.Font("Arial", 1, 15));
        btnEliminar.setText("Eliminar");
        btnEliminar.setPreferredSize(new java.awt.Dimension(80, 30));
        getContentPane().add(btnEliminar);
        btnEliminar.setBounds(200, 430, 120, 30);

        btnCancelar.setFont(new java.awt.Font("Arial", 1, 15));
        btnCancelar.setText("Cancelar");
        btnCancelar.setPreferredSize(new java.awt.Dimension(80, 30));
        getContentPane().add(btnCancelar);
        btnCancelar.setBounds(330, 430, 120, 30);

        btnSalir.setFont(new java.awt.Font("Arial", 1, 15));
        btnSalir.setText("Salir");
        getContentPane().add(btnSalir);
        btnSalir.setBounds(460, 430, 120, 30);
        getContentPane().add(dtdFecha);
        dtdFecha.setBounds(620, 10, 122, 28);

        jLabel7.setFont(new java.awt.Font("Arial", 0, 13));
        jLabel7.setText("Cantidad:");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(310, 160, 55, 30);

        txtCantidad.setFont(new java.awt.Font("Arial", 0, 13));
        getContentPane().add(txtCantidad);
        txtCantidad.setBounds(370, 160, 110, 30);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        this.setBounds(0, 0, 755, 565);
    }// </editor-fold>//GEN-END:initComponents

    private void txtCodigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCodigoActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_txtCodigoActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VistaOrden().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnConsultar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnGenerar;
    private javax.swing.JButton btnMas;
    private javax.swing.JButton btnMenos;
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JComboBox cmbproducto;
    private com.toedter.calendar.JDateChooser dtdFecha;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblIngrediente;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtCedula;
    private javax.swing.JTextField txtCodigo;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables



}
